var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    result: '',
    buthiddenCancel: false,
    orderStatus: [
      {
        statusName: '供应',
        statusCode: 1
      },
      {
        statusName: '求购',
        statusCode: 2
      }
    ],
    statusSelect: 1, //判断是否选中
    pageIndex: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //  that.onSupplyInfo();
  },

  onSupplyInfo: function() {
    var that = this
    var arr = new Array()
    wx.showLoading({
      title: '拼命加载中'
    })
    wx.request({
      method: 'POST',
      url: 'https://www.zaishengwang.com/wx/serchSupplyList.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        type: that.data.statusSelect,
        page: that.data.pageIndex,
        size: 8
      },
      success: function(res) {
        wx.hideLoading()
        if (res.data.pages.result.length > 0) {
          for (var i = 0; i < res.data.pages.result.length; i++) {
            let createTime = res.data.pages.result[i].createTime.substring(
              0,
              10
            )
            res.data.pages.result[i].createTime = createTime
          }
          arr = res.data.pages.result
          that.setData({ ifShownoData: true })
        } else {
          that.setData({ ifShownoData: false })
        }

        if (that.data.pageIndex != 1) {
          var array = that.data.result
          array = array.concat(arr)
          that.setData({ ifShownoData: true, result: array })
        } else {
          that.setData({ result: arr })
        }
      },
      fail: function(error) {
        console.log(error)
      }
    })
  },

  //详情页面
  goSupplyDetail: function(e) {
    console.log(e)
    var bId = e.currentTarget.dataset.bid
    var Type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: 'supply_detail?bId=' + bId + '&type=' + Type,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {}
    })
  },

  orderStatusList: function(e) {
    var that = this
    that.setData({
      //把选中值放入判断值
      statusSelect: e.currentTarget.dataset.select,
      pageIndex: 1,
      result: ''
    })
    that.onSupplyInfo()
  },
  //删除订单事件
  ifCancel: function(e) {
    var that = this
    var bId = e.currentTarget.dataset.bid

    wx.showModal({
      // title: '提示',
      content: '确定删除该条信息吗？',
      cancelText: '再等等',
      confirmColor: '#50BEC5',
      success: function(res) {
        if (res.confirm) {
          that.setData({ buthiddenCancel: true })
          wx.request({
            method: 'POST',
            url: 'https://www.zaishengwang.com/wx/toDeleteSupply.do',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              bId: bId
            },
            success: function(res) {
              if (res.data.msg != null) {
                that.setData({ buthiddenCancel: false })
              }
              that.setData({
                pageIndex: 1, // 每次触发下拉事件pageIndex=1
                result: ''
              })
              that.onSupplyInfo()
            },
            fail: function(error) {
              that.setData({ buthiddenCancel: false })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  goFbSupply: function() {
    var that = this
    wx.navigateTo({
      url: '../supply/fb_supply?type=' + that.data.statusSelect
    })
  },
  goModify: function(e) {
    var that = this
    var bId = e.currentTarget.dataset.bid
    wx.navigateTo({
      url:
        '../supply/modify_supply?bId=' + bId + '&type=' + that.data.statusSelect
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      result: ''
    })
    that.onSupplyInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      result: ''
    })
    that.onSupplyInfo()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    that.setData({
      pageIndex: that.data.pageIndex + 1
    })
    // 上拉获取更多数据
    that.onSupplyInfo()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    var that = this
    if (e.from === 'button') {
      // 来自页面内转发按钮
      var bId = e.target.dataset.bid;
      var Type = e.target.dataset.type;
      if (Type == 1) {
        su_type = "【供应】";
      } else {
        su_type = "【求购】";
      }
    }
    return {
      title: su_type + e.target.dataset.content,
      path: 'pages/supply/supply_detail?bId=' + bId + '&type=' + Type,
      success: function(res) {
        // 转发成功
        console.log('转发成功:' + JSON.stringify(e))
      },
      fail: function(res) {
        // 转发失败
        console.log('转发失败:' + JSON.stringify(e))
      }
    }
  }
})
